#include "RenderPch.h"

#include "Material.h"
#include "RenderFacade.h"
#include "ResourceManager.h"

Material::Material(void)
{
}

Material::~Material(void)
{
	RemoveAll();
}

BOOL Material::LoadFromXml( XmlNode * materialNode )
{
	m_name = materialNode->GetAttribute("name");

	std::string shaderTypeStr = materialNode->GetAttribute("shader_type"); 
	ShaderManager * shaderManager = RenderFacade::GetInstance()->Get3DRenderer()->GetShaderManager();
	m_shaderType = shaderManager->GetShaderTypeByString( shaderTypeStr );
	
	int dataNodeCount = materialNode->GetChildNodeCount("data");
	for( int cnt = 0; cnt < dataNodeCount; cnt++ )
	{
		XmlNode * dataNode = materialNode->GetChildNode("data", cnt);
		
		std::string name = dataNode->GetAttribute("name");
		std::string type = dataNode->GetAttribute("type");
		if( type == "texture" )
		{
			std::string path = dataNode->GetText();
			Texture * texture = ResourceManager::GetInstance()->CreateTexture(path.c_str(), TRUE);

			AddTextureData( name, texture );
		}
		else if( type == "int" )
		{
		}
		else if( type == "float" )
		{
		}
		else if( type == "dword" )
		{
		}
	}

	return TRUE;
}

void Material::AddIntData( std::string name, int data )
{
	m_materialDataList[name] = (DWORD)data;
}

void Material::AddFloatData( std::string name, float data )
{
	m_materialDataList[name] = *((DWORD*)&data);
}

void Material::AddDwordData( std::string name, DWORD data )
{
	m_materialDataList[name] = data;
}

void Material::AddTextureData( std::string name, Texture * data )
{
	m_textureDataList[name] = data;
}

int	Material::GetIntData( std::string name )
{
	std::map<std::string, DWORD>::iterator it = m_materialDataList.find( name );
	if( it != m_materialDataList.end() )
	{
		int result = (int)it->second;
		return result;
	}

	return -1;
}

float Material::GetFloatData( std::string name )
{
	std::map<std::string, DWORD>::iterator it = m_materialDataList.find( name );
	if( it != m_materialDataList.end() )
	{
		float result = *((float*)&it->second);
		return result;
	}

	return -1.f;
}

DWORD Material::GetDwordData( std::string name )
{
	std::map<std::string, DWORD>::iterator it = m_materialDataList.find( name );
	if( it != m_materialDataList.end() )
	{
		return it->second;
	}

	return static_cast< DWORD >( -1 );
}

Texture * Material::GetTextureData( std::string name )
{
	std::map<std::string, Texture*>::iterator it = m_textureDataList.find( name );
	if( it != m_textureDataList.end() )
	{
		Texture * result = (Texture *)it->second;
		return result;
	}

	return NULL;
}

void Material::RemoveAll()
{
	m_materialDataList.clear();

	std::map<std::string, Texture*>::iterator it =  m_textureDataList.begin();
	for( ; it != m_textureDataList.end(); it++ )
	{
		Texture * result = (Texture *)it->second;
		if( result )
		{
			ResourceManager::GetInstance()->ReleaseTexture( result->GetResourceName().c_str() );
		}
	}

	m_textureDataList.clear();
}